package com.vortex.cloud.zhsw.xinyang.manager;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.vortex.cloud.sdk.api.dto.ums.*;
import com.vortex.cloud.sdk.api.dto.ums.third.ThirdMessageSendDTO;
import com.vortex.cloud.sdk.api.service.IUmsService;
import com.vortex.cloud.zhsw.xinyang.dto.DictDataDTO;
import com.vortex.cloud.zhsw.xinyang.enums.ums.DivisionLevelEnum;
import com.vortex.cloud.zhsw.xinyang.support.Constants;
import org.apache.commons.compress.utils.Lists;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ums
 */
@Service
public class UmsManagerService {

    @Resource
    private IUmsService umsService;

    public static final String SDK_OBJECT_CODE_PARAM_SETTING = "PARAM_SETTING";
    public static final String SDK_OBJECT_CODE_DIVISION = "DIVISION";
    public static final String SDK_OBJECT_CODE_DEPT_ORG = "DEPT_ORG";
    public static final String SDK_OBJECT_CODE_STAFF = "STAFF";
    public static final String SDK_OBJECT_CODE_USER = "USER";

    public Map<String, DictDataDTO> mapDictData(String sdkObjectCode, String tenantId, String parameters) {
        switch (sdkObjectCode) {
            case SDK_OBJECT_CODE_PARAM_SETTING:
                return umsService.getByParamTypeCode(tenantId, parameters)
                        .stream()
                        .map(r -> new DictDataDTO(r.getParmCode(), r.getParmName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, r -> r, (r1, r2) -> r1));
            case SDK_OBJECT_CODE_DIVISION:
                return umsService.getDivisionList(tenantId, true, null, null)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            case SDK_OBJECT_CODE_DEPT_ORG:
                return umsService.loadDepartments(tenantId)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getText()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            case SDK_OBJECT_CODE_STAFF:
                return umsService.loadStaffsByFilter(tenantId)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            case SDK_OBJECT_CODE_USER:
                return umsService.getusersbycondiction(tenantId)
                        .stream()
                        .map(r -> new DictDataDTO(r.getId(), r.getStaffName()))
                        .collect(Collectors.toMap(DictDataDTO::getKey, Function.identity()));
            default:
                return Maps.newHashMap();
        }
    }


    /**
     * 用户列表
     *
     * @param tenantId 租户
     * @return 用户
     */
    public List<UserStaffDetailDTO> usersByTenantId(String tenantId) {
        Assert.hasText(tenantId, "租户ID不能为空");
        return umsService.getusersbycondiction(tenantId);
    }

    /**
     * 查询行政区划列表
     *
     * @param tenantId 租户
     * @return 行政区划
     */
    public List<DivisionDTO> divisionsByTenantId(String tenantId) {
        Assert.hasText(tenantId, "租户ID不能为空");
        return umsService.getDivisionList(tenantId, true, null, null);
    }

    /**
     * 根据租户id查询角色列表
     *
     * @param tenantId 租户id
     * @return 角色列表
     */
    public List<TenantRoleDTO> rolesByTenantId(@NotBlank String tenantId) {
        Assert.hasText(tenantId, "租户ID不能为空");

        return umsService.listAllTenantRoles(tenantId);
    }

    /**
     * 查出租户下所有部门
     *
     * @param tenantId 租户
     * @return 部门
     */
    public List<DeptOrgDetailDTO> orgsByTenantId(String tenantId) {
        Assert.hasText(tenantId, "租户ID不能为空");
        return umsService.findOrgList(tenantId);
    }

    public UserStaffDetailDTO getUserById(String tenantId, String userId) {
        if (StrUtil.isBlank(tenantId) || StrUtil.isBlank(userId)) {
            return null;
        }
        List<UserStaffDetailDTO> usersByTenantId = this.usersByTenantId(tenantId);
        if (CollUtil.isEmpty(usersByTenantId)) {
            return null;
        }

        Optional<UserStaffDetailDTO> first = usersByTenantId.stream().filter(userStaffDetailDTO -> userStaffDetailDTO.getId().equals(userId)).findFirst();
        return first.orElse(null);
    }

    /**
     * 根据租户和部门id获取子部门，包含自己本身
     *
     * @param tenantId
     * @param deptId
     * @return
     */
    public List<DeptOrgDetailDTO> getDeptList(String tenantId, String deptId) {
        List<DeptOrgDetailDTO> deptOrgList = umsService.findOrgList(tenantId);
        Map<String, DeptOrgDetailDTO> deptOrgMap = CollStreamUtil.toIdentityMap(deptOrgList, DeptOrgDetailDTO::getId);
        Map<String, List<DeptOrgDetailDTO>> deptParentMap = deptOrgMap.values().stream().collect(Collectors.groupingBy(DeptOrgDetailDTO::getParentId));
        DeptOrgDetailDTO dept = deptOrgMap.get(deptId);
        if (ObjectUtil.isNull(dept)) {
            return com.google.common.collect.Lists.newArrayList();
        }
        List<DeptOrgDetailDTO> result = getChildrenDeptDto(deptParentMap, deptId);
        result.add(dept);
        return result;
    }

    public List<DeptOrgDetailDTO> getChildrenDeptDto(Map<String, List<DeptOrgDetailDTO>> deptParentMap, String deptId) {
        List<DeptOrgDetailDTO> result = com.google.common.collect.Lists.newArrayList();
        List<DeptOrgDetailDTO> deptDtoList = deptParentMap.get(deptId);
        if (CollUtil.isEmpty(deptDtoList)) {
            return result;
        }
        result.addAll(deptDtoList);
        deptDtoList.forEach(d -> result.addAll(getChildrenDeptDto(deptParentMap, d.getId())));
        return result;
    }

    /**
     * 根据租户id和用户id列表查询用户信息
     *
     * @param tenantId 租户id
     * @param idList   id列表
     * @return 用户信息列表
     */
    public List<UserStaffDetailDTO> getUserByIds(String tenantId, List<String> idList) {
        if (StrUtil.isBlank(tenantId)) {
            return null;
        }
        List<UserStaffDetailDTO> usersByTenantId = this.usersByTenantId(tenantId);
        if (CollUtil.isEmpty(usersByTenantId)) {
            return null;
        }
        if (CollUtil.isEmpty(idList)) {
            return usersByTenantId;
        }
        return usersByTenantId.stream().filter(a -> idList.contains(a.getId())).collect(Collectors.toList());
    }

    /**
     * @param tenantId 租户
     * @param userId   用户id
     * @return 用户名称
     */
    public String getUserNameById(String tenantId, String userId) {
        UserStaffDetailDTO userById = this.getUserById(tenantId, userId);
        if (userById == null) {
            return null;
        }
        return userById.getStaffName();
    }

    /**
     * @param tenantId   租户
     * @param divisionId 行政区划id
     * @return 行政区划dto
     */
    public DivisionDTO getDivisionById(@NotBlank String tenantId, @NotBlank String divisionId) {
        if (StrUtil.isBlank(tenantId) || StrUtil.isBlank(divisionId)) {
            return null;
        }
        List<DivisionDTO> all = this.divisionsByTenantId(tenantId);
        if (CollUtil.isEmpty(all)) {
            return null;
        }
        Optional<DivisionDTO> first = all.stream().filter(item -> item.getId().equals(divisionId)).findFirst();
        return first.orElse(null);
    }

    /**
     * @param tenantId   租户
     * @param divisionId 行政区划id
     * @return 行政区划名称
     */
    public String getDivisionNameById(@NotBlank String tenantId, @NotBlank String divisionId) {
        DivisionDTO divisionDTO = this.getDivisionById(tenantId, divisionId);
        return divisionDTO == null ? null : divisionDTO.getName();
    }

    /**
     * @param tenantId     租户
     * @param divisionName 行政区划名称
     * @return 行政区划dto
     */
    public DivisionDTO getDivisionByName(@NotBlank String tenantId, @NotBlank String divisionName) {
        if (StrUtil.isBlank(tenantId) || StrUtil.isBlank(divisionName)) {
            return null;
        }
        List<DivisionDTO> all = this.divisionsByTenantId(tenantId);
        if (CollUtil.isEmpty(all)) {
            return null;
        }
        Optional<DivisionDTO> first = all.stream().filter(item -> item.getName().equals(divisionName)).findFirst();
        return first.orElse(null);
    }

    /**
     * 根据租户id和行政区划名称查询行政区划id
     *
     * @param tenantId     租户
     * @param divisionName 行政区划名称
     * @return 行政区划id
     */
    public String getDivisionIdByName(@NotBlank String tenantId, @NotBlank String divisionName) {
        DivisionDTO divisionDTO = this.getDivisionByName(tenantId, divisionName);
        return divisionDTO == null ? null : divisionDTO.getId();
    }

    /**
     * @param tenantId 租户
     * @param id       部门id
     * @return 部门名称
     */
    public String getOrgNameById(String tenantId, String id) {
        if (StrUtil.isBlank(tenantId) || StrUtil.isBlank(id)) {
            return null;
        }
        List<DeptOrgDetailDTO> all = this.orgsByTenantId(tenantId);
        if (CollUtil.isEmpty(all)) {
            return null;
        }
        Optional<DeptOrgDetailDTO> first = all.stream().filter(item -> item.getId().equals(id)).findFirst();
        return first.map(DeptOrgDetailDTO::getName).orElse(null);
    }

    /**
     * @param tenantId 租户
     * @param orgName  部门名称
     * @return 部门id
     */
    public String getOrgIdByName(String tenantId, String orgName) {
        if (StrUtil.isBlank(tenantId) || StrUtil.isBlank(orgName)) {
            return null;
        }
        List<DeptOrgDetailDTO> all = this.orgsByTenantId(tenantId);
        if (CollUtil.isEmpty(all)) {
            return null;
        }
        Optional<DeptOrgDetailDTO> first = all.stream().filter(item -> item.getName().equals(orgName)).findFirst();
        return first.map(DeptOrgDetailDTO::getId).orElse(null);
    }

    /**
     * 根据角色code获取角色名称
     *
     * @param tenantId 租户id
     * @param roleCode 角色编码
     * @return 角色名称
     */
    public String getRoleNameByCode(String tenantId, String roleCode) {
        if (StrUtil.hasBlank(tenantId, roleCode)) {
            return null;
        }
        List<TenantRoleDTO> tenantRoleList = this.rolesByTenantId(tenantId);
        if (CollUtil.isEmpty(tenantRoleList)) {
            return null;
        }
        Optional<TenantRoleDTO> first = tenantRoleList.stream().filter(tenantRoleDTO -> roleCode.equals(tenantRoleDTO.getCode())).findFirst();
        return first.map(TenantRoleDTO::getName).orElse(null);
    }

    /**
     * 获取用户所在部门/机构
     *
     * @param tenantId 租户id
     * @param userId   用户id
     * @return 部门id
     */
    public String getUserOrgId(@NotBlank String tenantId, @NotBlank String userId) {
        UserStaffDetailDTO userById = this.getUserById(tenantId, userId);
        if (userById == null) {
            return null;
        }
        String orgId = userById.getOrgId();
        String departmentId = userById.getDepartmentId();
        return StrUtil.isNotBlank(orgId) ? orgId : departmentId;
    }

    /**
     * 查询parentId行政区划关联的行政区划
     *
     * @param tenantId 租户id
     * @param parentId 父级行政区划
     * @param level    行政区划级别
     * @return 行政区划
     */
    public Set<String> getDivisionIdsByParentAndLevel(String tenantId, @Nullable String parentId, @Nullable DivisionLevelEnum level) {
        Assert.hasText(tenantId, "租户ID不能为空");
        List<DivisionDTO> allDivisions = this.divisionsByTenantId(tenantId);
        if (CollUtil.isEmpty(allDivisions)) {
            return new HashSet<>(0);
        }
        Map<String, String> id2ParentId = allDivisions.stream().collect(Collectors.toMap(DivisionDTO::getId, DivisionDTO::getParentId, (k1, k2) -> k1));
        return allDivisions.stream().filter(divisionDTO -> !StrUtil.isNotBlank(parentId) || this.isParentOrSelf(divisionDTO.getId(), parentId, id2ParentId)).filter(divisionDTO -> !Objects.nonNull(level) || !Integer.valueOf(level.getKey()).equals(divisionDTO.getLevel())).map(DivisionDTO::getId).collect(Collectors.toSet());
    }

    /**
     * 判断childId是否是parentId的子级,或者childId=parentId
     *
     * @param childId     子级id
     * @param parentId    父级id
     * @param id2ParentId ID到parentId映射
     * @return 返回
     */
    private boolean isParentOrSelf(String childId, String parentId, Map<String, String> id2ParentId) {
        if (StrUtil.isBlank(childId) || StrUtil.isBlank(parentId) || CollUtil.isEmpty(id2ParentId)) {
            return false;
        }
        if (childId.equals(parentId)) {
            return true;
        }
        String thisParentId = id2ParentId.get(childId);

        if (StrUtil.isBlank(thisParentId)) {
            return false;
        }
        if (thisParentId.equals(childId)) {
            return false;
        }
        if (thisParentId.equals(parentId)) {
            return true;
        }
        return this.isParentOrSelf(thisParentId, parentId, id2ParentId);
    }

    public DivisionLevelEnum getDivisionLevel(@NotBlank String tenantId, @NotBlank String divisionId) {
        DivisionDTO divisionDTO = this.getDivisionById(tenantId, divisionId);
        Integer level = divisionDTO.getLevel();
        return DivisionLevelEnum.getByKey(level);
    }

    /**
     * 获取当前行政区划指定级别的父级Id
     *
     * @param tenantId       租户id
     * @param thisDivisionId 行政区划id
     * @param divisionLevel  行政区划级别
     * @return 父级行政区划id
     */
    public String getParentDivisionIdByLevel(@NotBlank String tenantId, @NotBlank String thisDivisionId, @NotNull DivisionLevelEnum divisionLevel) {
        DivisionDTO divisionDTO = getDivisionById(tenantId, thisDivisionId);
        if (divisionDTO == null || divisionDTO.getLevel() == null) {
            return null;
        }
        DivisionLevelEnum thisDivisionLevel = DivisionLevelEnum.getByKey(divisionDTO.getLevel());
        Assert.notNull(thisDivisionLevel, "行政区划级别获取错误");
        if (divisionLevel == thisDivisionLevel) {
            return thisDivisionId;
        }
        if (StrUtil.isBlank(divisionDTO.getParentId())) {
            return null;
        }
        return getParentDivisionIdByLevel(tenantId, divisionDTO.getParentId(), divisionLevel);
    }

    public Set<String> getDivisionIdByParentId(String tenantId, String parentId, Boolean all) {
        List<DivisionDTO> divisions = umsService.getDivisionList(tenantId, true, null, null);
        Set<String> parentIds = Sets.newHashSet(parentId);
        parentIds = setDivisionIdData(parentIds, divisions, all);
        parentIds.add(parentId);
        return parentIds;
    }

    public Set<String> getOrgIdByParentId(String tenantId, String parentId, Boolean all) {
        List<DeptOrgDetailDTO> orgs = umsService.findOrgList(tenantId);
        Set<String> parentIds = Sets.newHashSet(parentId);
        parentIds = setOrgIdData(parentIds, orgs, all);
        parentIds.add(parentId);
        return parentIds;
    }

    private Set<String> setOrgIdData(Set<String> parentIds, List<DeptOrgDetailDTO> orgs, Boolean all) {
        Iterator<DeptOrgDetailDTO> iterator = orgs.iterator();
        Set<String> nextSet = Sets.newHashSet();
        Set<String> result = Sets.newHashSet();
        while (iterator.hasNext()) {
            DeptOrgDetailDTO orgDTO = iterator.next();
            if (parentIds.contains(orgDTO.getParentId())) {
                result.add(orgDTO.getId());
                nextSet.add(orgDTO.getId());
                iterator.remove();
            }
        }
        if (all) {
            if (!CollectionUtils.isEmpty(nextSet)) {
                result.addAll(setOrgIdData(nextSet, orgs, true));
            } else {
                return result;
            }
        } else {
            return result;
        }
        return result;
    }

    /**
     * 获取下级行政区DTO（不含本级）
     *
     * @param tenantId 租户id
     * @param parentId 父级行政区划id
     * @return 下级行政区划列表
     */
    public Set<DivisionDTO> getDivisionIdByParentIdNew(String tenantId, String parentId) {
        List<DivisionDTO> divisions = umsService.getDivisionList(tenantId, true, null, null);
        Set<String> parentIds = Sets.newHashSet(parentId);
        return setDivisionData(parentIds, divisions);
    }

    private Set<DivisionDTO> setDivisionData(Set<String> parentIds, List<DivisionDTO> divisions) {
        Iterator<DivisionDTO> iterator = divisions.iterator();
        Set<DivisionDTO> result = Sets.newHashSet();
        while (iterator.hasNext()) {
            DivisionDTO divisionDTO = iterator.next();
            if (parentIds.contains(divisionDTO.getParentId())) {
                result.add(divisionDTO);
                iterator.remove();
            }
        }
        return result;
    }

    private Set<String> setDivisionIdData(Set<String> parentIds, List<DivisionDTO> divisions, Boolean all) {
        Iterator<DivisionDTO> iterator = divisions.iterator();
        Set<String> nextSet = Sets.newHashSet();
        Set<String> result = Sets.newHashSet();
        while (iterator.hasNext()) {
            DivisionDTO divisionDTO = iterator.next();
            if (parentIds.contains(divisionDTO.getParentId())) {
                result.add(divisionDTO.getId());
                nextSet.add(divisionDTO.getId());
                iterator.remove();
            }
        }
        if (all) {
            if (!CollectionUtils.isEmpty(nextSet)) {
                result.addAll(setDivisionIdData(nextSet, divisions, true));
            } else {
                return result;
            }
        } else {
            return result;
        }
        return result;
    }


    public Map<String, String> divisionIdNameMap(String tenantId, Boolean containsRoot, String parentId, Integer level) {
        List<DivisionDTO> divisionList = umsService.getDivisionList(tenantId, containsRoot, parentId, level);
        if (!CollectionUtils.isEmpty(divisionList)) {
            return divisionList.stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toMap(DivisionDTO::getId, DivisionDTO::getName));
        } else {
            return new HashMap<>(16);
        }
    }

    public Map<String, String> divisionNameIdMap(String tenantId, Boolean containsRoot, String parentId, Integer level) {
        List<DivisionDTO> divisionList = umsService.getDivisionList(tenantId, containsRoot, parentId, level);
        if (!CollectionUtils.isEmpty(divisionList)) {
            return divisionList.stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toMap(DivisionDTO::getName, DivisionDTO::getId, (x1, x2) -> x1));
        } else {
            return new HashMap<>(16);
        }
    }

    public Map<String, String> orgIdNameMap(String tenantId) {
        List<DeptOrgDetailDTO> list = this.orgsByTenantId(tenantId);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return list.stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toMap(DeptOrgDetailDTO::getId, DeptOrgDetailDTO::getName));
    }

    public Map<String, String> orgNameIdMap(String tenantId) {
        List<DeptOrgDetailDTO> list = this.orgsByTenantId(tenantId);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return list.stream().filter(a -> Objects.nonNull(a.getId())).collect(Collectors.toMap(DeptOrgDetailDTO::getName, DeptOrgDetailDTO::getId, (v1, v2) -> v2));
    }

    public Set<String> hasFunctions(String userId, Set<String> functionCodes) {
        return umsService.hasFunctions(userId, functionCodes);
    }

    /**
     * 根据租户查询用户信息详情
     *
     * @param tenantId 租户ID
     * @return 用户信息
     */
    public Map<String, UserStaffDetailDTO> getUserStaffDetailMapByTenantId(String tenantId) {
        List<UserStaffDetailDTO> userList = usersByTenantId(tenantId);
        if (CollUtil.isNotEmpty(userList)) {
            return userList.stream().collect(Collectors.toMap(UserStaffDetailDTO::getId, t -> t, (e1, e2) -> e1));
        }
        return new HashMap<>(16);
    }

    /**
     * 发送消息
     *
     * @param messageSendDTO 发送消息
     */
    public void sendMsg(ThirdMessageSendDTO messageSendDTO) {
        umsService.sendThirdMessage(messageSendDTO);
    }

    public List<UserStaffDetailDTO> getUserStaffByFunctionCode(String tenantId, String userId, String functionCode) {
        List<UserStaffDetailDTO> list = Lists.newArrayList();
        List<UserStaffDetailDTO> dtoList = usersByTenantId(tenantId);
        for (UserStaffDetailDTO dto : dtoList) {
            if (StrUtil.isNotEmpty(functionCode)) {
                Set<String> set = hasFunctions(dto.getId(), Sets.newHashSet(functionCode));
                if (CollUtil.isEmpty(set)) {
                    continue;
                }
            }
            if (!userId.equals(dto.getId())) {
                continue;
            }
            list.add(dto);
        }
        for (UserStaffDetailDTO dto : dtoList) {
            if (StrUtil.isNotEmpty(functionCode)) {
                Set<String> set = hasFunctions(dto.getId(), Sets.newHashSet(functionCode));
                if (CollUtil.isEmpty(set)) {
                    continue;
                }
            }
            if (userId.equals(dto.getId())) {
                continue;
            }
            list.add(dto);
        }
        return list;
    }

    public UserStaffDetailDTO getUserStaffById(String tenantId, String userId) {
        return getUserById(tenantId, userId);
    }

    /**
     * 根据租户查询用户ID
     *
     * @param tenantId 租户ID
     * @return staffId:userId
     */
    public Map<String, String> getUserMapByTenantId(String tenantId) {
        List<UserStaffDetailDTO> userList = usersByTenantId(tenantId);
        if (CollUtil.isNotEmpty(userList)) {
            return userList.stream().collect(Collectors.toMap(UserStaffDetailDTO::getStaffId, UserStaffDetailDTO::getId, (e1, e2) -> e1));
        }
        return new HashMap<>(16);
    }

    /**
     * 根据租户id和用户人员id列表查询用户信息
     *
     * @param tenantId 租户id
     * @param idList   id列表
     * @return 用户信息列表
     */
    public Map<String, String> getUserMapByIds(String tenantId, Set<String> idList) {
        if (StrUtil.isBlank(tenantId)) {
            return null;
        }
        List<UserStaffDetailDTO> usersByTenantId = this.usersByTenantId(tenantId);
        if (CollUtil.isEmpty(usersByTenantId)) {
            return null;
        }
        if (CollUtil.isEmpty(idList)) {
            return null;
        }
        Map<String, String> userMap = Maps.newHashMap();
        for (UserStaffDetailDTO dto : usersByTenantId) {
            if (!idList.contains(dto.getStaffId())) {
                continue;
            }
            userMap.put(dto.getStaffId(), dto.getStaffName());
        }
        return userMap;
    }

    /**
     * 校验登录信息
     * @param request HttpServletRequest
     * @return 登录用户信息
     */
    public UserStaffDetailDTO getLoginInfo(HttpServletRequest request) {
        Assert.isTrue(StrUtil.isNotEmpty(request.getHeader(Constants.USER_ID)),"用户id不能为空");
        Assert.isTrue(StrUtil.isNotEmpty(request.getHeader(Constants.TENANT_ID)),"租户id不能为空");
        // 获取用户信息
        UserStaffDetailDTO detail = this.getUserById(request.getHeader(Constants.TENANT_ID), request.getHeader(Constants.USER_ID));
        Assert.isTrue(ObjectUtil.isNotEmpty(detail), "登录用户信息为空");
        return detail;
    }

    /**
     *  根据参数编码查询参数值
     * @param tenantId 租户id
     * @param paramCode 参数编码
     * @return
     */
    public Map<String, String> getParamsByCode(String tenantId, String paramCode){
        List<ParamSettingDTO> dtoList = umsService.getByParamTypeCode(tenantId, paramCode);
        if (CollUtil.isEmpty(dtoList)){
            return null;
        }
       return dtoList.stream().collect(Collectors.toMap(ParamSettingDTO::getParmCode, ParamSettingDTO::getParmName, (a, b) -> b));
    }
}

